from flask import Blueprint, render_template, redirect, url_for, session, flash
from auth import login_required, get_db_connection

simple_user_bp = Blueprint('simple_user', __name__)

@simple_user_bp.route('/simple_book')
@login_required
def simple_book():
    """简易房间预订页面"""
    conn = get_db_connection()
    cursor = conn.cursor(dictionary=True)
    cursor.execute("SELECT * FROM rooms WHERE status = 'available'")
    rooms = cursor.fetchall()
    cursor.close()
    conn.close()

    return render_template('user/simple_book.html', rooms=rooms)

@simple_user_bp.route('/my_bookings')
@login_required
def my_bookings():
    """我的预订列表"""
    conn = get_db_connection()
    cursor = conn.cursor(dictionary=True)
    
    # 获取用户预订信息
    cursor.execute("""
        SELECT b.*, r.name as room_name, r.price, r.room_type
        FROM bookings b 
        JOIN rooms r ON b.room_id = r.id 
        WHERE b.user_id = %s 
        ORDER BY b.created_at DESC
    """, (session['user_id'],))
    bookings = cursor.fetchall()
    
    cursor.close()
    conn.close()
    
    return render_template('user/my_bookings.html', bookings=bookings)